package Gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComboBox;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JButton;

import Logic.LoginInfo;

import client.LibaryMain;
import javax.swing.ImageIcon;

/**
 * this class implements the search gui 
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */

public class Search extends JPanel {
	private JTextField SearchtextField;
	/**
	 * Contractor of search 
	 * 
	 */
	public Search() {
		setBackground(new Color(153, 204, 255));
		init();
	}
	/**
	 * init the search window 
	 */
			
	public void init(){
	
		
		JLabel lblSearchTopic = new JLabel("Search Topic");
		lblSearchTopic.setBounds(10, 43, 164, 20);
		lblSearchTopic.setFont(new Font("Tahoma", Font.BOLD, 16));
		
		final JComboBox TopiccomboBox = new JComboBox();
		TopiccomboBox.setBounds(178, 38, 130, 31);
		TopiccomboBox.setFont(new Font("Tahoma", Font.BOLD, 16));
		String[] str ;  

			TopiccomboBox.addItem("Title");
			TopiccomboBox.addItem("Author");
			TopiccomboBox.addItem("Subject");
	
		
		SearchtextField = new JTextField();
		SearchtextField.setBounds(332, 38, 611, 31);
		SearchtextField.setFont(new Font("Tahoma", Font.BOLD, 14));
		SearchtextField.setColumns(10);
		
		JButton btnConfirmSearch = new JButton("Confirm");
		btnConfirmSearch.setBounds(808, 98, 116, 37);
		btnConfirmSearch.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnConfirmSearch.addActionListener(new java.awt.event.ActionListener() {     
            public void actionPerformed(java.awt.event.ActionEvent ev) {
            	btnConfirm(ev,TopiccomboBox);
            					
			}

			private void btnConfirm(ActionEvent ev,JComboBox<String> TopiccomboBox ) {
				ArrayList<String> arr=new ArrayList<String>();
				String stf= SearchtextField.getText();
				System.out.println("######################"+stf);
				if (stf.equals("") ) 
				{
					JOptionPane.showMessageDialog(null,"Please enter text to search");
				}
				else  {
					arr.add("Search");
					arr.add((String) TopiccomboBox.getSelectedItem());
					arr.add(stf);
					System.out.println(arr);
					LibaryMain.chat.handleMessageFromGui(arr);
				}
				
			}
        });
		
		JButton btnLogout = new JButton("Logout");
		btnLogout.setBounds(874, 509, 97, 25);
		btnLogout.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ArrayList<String> Arr=new ArrayList<String>();
				Arr.add("Logout");
				Arr.add("Subscriber");
				Arr.add(LoginInfo.UserName);
				LibaryMain.chat.handleMessageFromGui(Arr);
				MainScreen.cardLayot.show(MainScreen.card, "Login");
				
			}
		});
		
		JButton btnBack = new JButton("Back");
		btnBack.setBounds(84, 509, 90, 25);
		btnBack.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnBack.addActionListener(new java.awt.event.ActionListener() {     
            public void actionPerformed(java.awt.event.ActionEvent ev) {
            	btnback(ev);
            					
			}

			private void btnback(ActionEvent ev) {
				MainScreen.cardLayot.show(MainScreen.card, DirectorWindow.roll);
				
			}
        });
		setLayout(null);
		add(lblSearchTopic);
		add(TopiccomboBox);
		add(SearchtextField);
		add(btnConfirmSearch);
		add(btnBack);
		add(btnLogout);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(Search.class.getResource("/Gui/images/Search.png")));
		label.setBounds(264, 146, 600, 418);
		add(label);

	}
	/**
	 * showing the search results 
	 * @param str with the results
	 */
		public void ShowResult(String [] str) 
		{
			System.out.println("AVIADAVIADAVIAD"+str[0]);
			SearchResult sr= new SearchResult(str);
			MainScreen.card.add("SearchResult", sr);
			MainScreen.cardLayot.show(MainScreen.card, "SearchResult");
		}
		
	}

